﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IAlertsB;
using StartsEAS_API.Model;

namespace StartsEAS_API.Controllers.AlertsContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class AlertsController : ControllerBase
    {
        private readonly IAlertsBLL _alertsBll;
        public AlertsController(IAlertsBLL alertsBll)
        {
            _alertsBll = alertsBll;
        }
        /// <summary>
        /// 查询所有消息通知
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllAlerts() {
            try
            {
                return _alertsBll.GetAllAlert();
            }
            catch (System.Exception e) {
                AppLog.WriteError($"GetAllAlerts服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加消息通知
        /// </summary>
        /// <param name="data">消息JSON数据</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertAlerts(JObject data) {
            try
            {
                return _alertsBll.InsertAlerts(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertAlerts服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改消息通知数据
        /// </summary>
        /// <param name="data">需要修改的数据</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils UpdateAlerts(JObject data)
        {
            try
            {
                return _alertsBll.UpdateAlerts(data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"UpdateAlerts服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        
        /// <summary>
        /// 查询学生可见的消息
        /// </summary>
        /// <returns></returns>
        //[HttpGet]
        //public ResultUtils GetAllAlertByStudent() { 
        //    return _alertsBll.GetAllAlertByStudent();
        //}

        [HttpGet]
        public ResultUtils GetAllAlertByToken(string token) {
            try
            {
                return _alertsBll.GetAllAlertByToken(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllAlertByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
